package com.example.health_life.service;

import com.example.health_life.dao.Course;
import com.example.health_life.dao.CourseRepository;
import com.example.health_life.dto.UsersDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    
    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private UserService userService;

    @Override
    public List<Course> getCourseList() {
        return courseRepository.findAll();
    }

    @Override
    public List<Course> getCourseListWithAuthorByStatus(Integer status) {
        List<Course> courses;
        if (status == 0) {
            courses = courseRepository.findAll();
        } else {
            courses = courseRepository.findByStatus((byte) status.intValue());
        }
        for (Course course : courses) {
            UsersDTO author = userService.getUserById(course.getAuthorId());
            if (author != null) {
                course.setAuthorNickname(author.getNickname());
                course.setAuthorAvatar(author.getAvatar());
            }
        }
        return courses;
    }

    @Override
    public Course getCourseById(Integer id) {
        return courseRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("课程不存在"));
    }

    @Override
    public List<Course> getCourseListByLevel(Byte level) {
        return courseRepository.findByLevel(level);
    }

    @Override
    public int addCourse(Course course) {
        // 设置默认值
        if (course.getFavorite() == null) {
            course.setFavorite(0);
        }
        // 设置创建时间
        course.setCreateTime(java.time.Instant.now());
        // 设置状态为正常
        course.setStatus((byte) 1);
        return courseRepository.save(course).getId();
    }

    @Override
    public int deleteCourse(Integer id) {
        // 判断课程是否存在
        Course course = courseRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("课程不存在"));

        // 使用 updateCourse 方法更新状态
        byte status = courseRepository.findById(id).get().getStatus();
        return updateCourse(id, null, null, null, null, null, (byte) (-1*status));
    }
    @Override
    public int updateCourse(Integer id, String title, String introduce, String coverImg, 
                          Byte level, Integer favorite, Byte status) {
        // 判断课程是否存在
        Course course = courseRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("课程不存在"));

        // 更新课程信息
        if (title != null) {
            course.setTitle(title);
        }
        if (introduce != null) {
            course.setIntroduce(introduce);
        }
        if (coverImg != null) {
            course.setCoverImg(coverImg);
        }
        if (level != null) {
            course.setLevel(level);
        }
        if (favorite != null) {
            course.setFavorite(favorite);
        }
        if (status != null) {
            course.setStatus(status);
        }

        courseRepository.save(course);
        return id;
    }

    @Override
    public Course getCourseByIdWithAuthor(Integer id) {
        Course course = courseRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("课程不存在"));
        UsersDTO author = userService.getUserById(course.getAuthorId());
        if (author != null) {
            course.setAuthorNickname(author.getNickname());
            course.setAuthorAvatar(author.getAvatar());
        }
        return course;
    }

    @Override
    public List<Course> getCourseListByLevelWithAuthor(Byte level) {
        List<Course> courses = courseRepository.findByLevel(level);
        for (Course course : courses) {
            UsersDTO author = userService.getUserById(course.getAuthorId());
            if (author != null) {
                course.setAuthorNickname(author.getNickname());
                course.setAuthorAvatar(author.getAvatar());
            }
        }
        return courses;
    }
}
